package org.cycuchy.kapsel {
	
	import com.hydrotik.queueloader.QueueLoader;
	import com.hydrotik.queueloader.QueueLoaderConst;
	import com.hydrotik.queueloader.QueueLoaderEvent;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.LoaderInfo;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	
	import mx.containers.Canvas;
	import mx.controls.Image;
	import mx.events.FlexEvent;
	
	import qs.containers.Landscape;
	import qs.graphics.Hilight;

	public class KLandscapeCanvas extends Canvas {
		
		private var land:Landscape;
		private var _items:Array = new Array();
		private var items:Array;		
		private var _bitmap:BitmapData;
		private var cnt:int;
		private var ii:int = 0;
		
		
		//private var _txt:Text;
		
		public function txt():String {
			var i:uint = 0;
			var s:String = "<font color='#000000' size='+5'>";
			for each (var item:Object in items) {
				s += "<a href='event:" + i + "'>" + item.text + "</a>&nbsp;&nbsp;";
				i++;
			}
			s += "</font>";
			return s;
		} 
		
//        [Embed(source='assets/places.jpg')]
//		private static const placesImageClass:Class;
		
		public function KLandscapeCanvas() {
			super();
			addEventListener(FlexEvent.CREATION_COMPLETE, creationComplete);
			items = [
				{text: "00", x: -1, y: -1, width: -1, height: -1},	
				{text: "52", x: 272, y: 1187, width: 271, height: 136},
				{text: "01", x: 0, y: 0, width: 240, height: 155},
				{text: "51", x: 14, y: 1202, width: 264, height: 131},
				{text: "02", x: 292, y: 240, width: 105, height: 77},
				{text: "50", x: 748, y: 1039, width: 134, height: 283},
				{text: "03", x: 240, y: 20, width: 260, height: 130},
				{text: "49", x: 541, y: 1225, width: 210, height: 75},
				{text: "04", x: 480, y: 20, width: 125, height: 60},
				{text: "48", x: 545, y: 1138, width: 205, height: 89},
				{text: "05", x: 600, y: 20, width: 90, height: 175},
				{text: "47", x: 546, y: 1045, width: 206, height: 93},
				{text: "06", x: 690, y: 20, width: 20, height: 240},
				{text: "46", x: 267, y: 1064, width: 282, height: 124},
				{text: "07", x: 710, y: 20, width: 20, height: 240},
				{text: "45", x: 0, y: 1069, width: 266, height: 135},
				{text: "08", x: 733, y: 20, width: 20, height: 240},
				{text: "44", x: 625, y: 929, width: 233, height: 112},
				{text: "9", x: 750, y: 10, width: 140, height: 242},
				{text: "43", x: 353, y: 927, width: 270, height: 127},
				{text: "10", x: 0, y: 156, width: 182, height: 265},
				{text: "42", x: 676, y: 853, width: 163, height: 78},
				{text: "11", x: 183, y: 155, width: 126, height: 268},
				{text: "41", x: 532, y: 851, width: 144, height: 73},
				{text: "12", x: 304, y: 147, width: 199, height: 86},
				{text: "40", x: 631, y: 756, width: 205, height: 96},
				{text: "13", x: 500, y: 79, width: 102, height: 141},
				{text: "39", x: 529, y: 786, width: 102, height: 67},
				{text: "14", x: 394, y: 226, width: 183, height: 90},
				{text: "38", x: 343, y: 786, width: 187, height: 140},
				{text: "15", x: 572, y: 212, width: 71, height: 107},
				{text: "37", x: 156, y: 784, width: 193, height: 284},
				{text: "16", x: 305, y: 315, width: 210, height: 106},
				{text: "36", x: 0, y: 791, width: 159, height: 278},
				{text: "17", x: 513, y: 320, width: 137, height: 90},
				{text: "35", x: 455, y: 744, width: 175, height: 42},
				{text: "18", x: 643, y: 260, width: 121, height: 150},
				{text: "34", x: 836, y: 661, width: 67, height: 118},
				{text: "19", x: 771, y: 259, width: 134, height: 198},
				{text: "33", x: 636, y: 660, width: 201, height: 97},
				{text: "20", x: 0, y: 425, width: 189, height: 130},
				{text: "32", x: 451, y: 671, width: 186, height: 73},
				{text: "21", x: 190, y: 423, width: 173, height: 123},
				{text: "31", x: 393, y: 677, width: 64, height: 101},
				{text: "22", x: 529, y: 523, width: 206, height: 138},
				{text: "30", x: 195, y: 687, width: 198, height: 93},
				{text: "23", x: 730, y: 521, width: 56, height: 140},
				{text: "29", x: 0, y: 689, width: 195, height: 101},
				{text: "24", x: 789, y: 458, width: 98, height: 201},
				{text: "28", x: 267, y: 537, width: 264, height: 138},
				{text: "25", x: 364, y: 420, width: 204, height: 111},
				{text: "27", x: 0, y: 550, width: 263, height: 139},
				{text: "26", x: 566, y: 407, width: 207, height: 112},
			];			
		}
		
		private var _oLoader:QueueLoader;
		
		private function creationComplete(event:FlexEvent):void {
			removeEventListener(FlexEvent.CREATION_COMPLETE, creationComplete);
			
            _bitmap = new BitmapData(1500, 1500, false, 0x000000FF);
//            var image:Bitmap = new Bitmap(_bitmap);
            
			_oLoader = new QueueLoader();            
            
			var s:String;
			cnt = 59;
			for (var idx:int=1; idx<cnt; idx++) {
				if (idx < 10) {
					s = "0";
				} else {
					s = "";
				}
				s += idx;
				
				_oLoader.addItem("assets/kapsle/" + s + ".jpg", null, {mimeType: QueueLoaderConst.FILE_IMAGE});
//				var loader:Loader = new Loader();
//    	        loader.contentLoaderInfo.addEventListener(Event.COMPLETE, getHandler(idx));
//    	        loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, iOError);
//        	    loader.load(new URLRequest("assets/kapsle/" + s + ".jpg"));
			}
			_oLoader.addEventListener(QueueLoaderEvent.ITEM_COMPLETE, onItemComplete, false, 0, true);
			_oLoader.addEventListener(QueueLoaderEvent.ITEM_ERROR, onItemError, false, 0, true);
			_oLoader.addEventListener(QueueLoaderEvent.QUEUE_COMPLETE, onQueueComplete, false, 0, true);
			_oLoader.execute();
			
//			land = Landscape(getChildByName("land"));
//			var polaroid:Image = new Image();
//			polaroid.source = image;
//			_items.push(polaroid);
//			view(polaroid);
//			
//			var c:Hilight;
//			var item:Object;
//			for (var i:uint=1; i<items.length; i++) {
//				item = items[i];
//				c = new Hilight();
//				c.x = item.x;
//				c.y = item.y;
//				c.width = item.width;
//				c.height = item.height;
//				_items.push(c); 
//			}
//			land.children = _items;
		}
		
		private function onItemComplete(event:QueueLoaderEvent):void {
			trace("\t>> "+event.type, "item idx: "+event.index);
			createBitmap(Bitmap(event.content));
		}	
		
		private function onItemError(event:QueueLoaderEvent):void {
        	trace("\n>>"+event.message+"\n");
		}
		
		private function onQueueComplete(event:QueueLoaderEvent):void {
			trace("onQueueComplete");
			_oLoader.dispose();
			
            var image:Bitmap = new Bitmap(_bitmap);
			land = Landscape(getChildByName("land"));
			var polaroid:Image = new Image();
			polaroid.source = image;
			_items.push(polaroid);
			view(polaroid);
			
			var c:Hilight;
			var item:Object;
			for (var i:uint=1; i<items.length; i++) {
				item = items[i];
				c = new Hilight();
				c.x = item.x;
				c.y = item.y;
				c.width = item.width;
				c.height = item.height;
				_items.push(c); 
			}
			land.children = _items;
			
			
		}		
		
//		override protected function childrenCreated():void {
//			super.childrenCreated();
//			var i:uint = 0;
//			var s:String = "<font color='#FFFFFF' size='+5'>";
//			for each (var item:Object in items) {
//				s += "<a href='event:" + i + "'>" + item.text + "</a>&nbsp;&nbsp;";
//				i++;
//			}
//			s += "</font>";
//			txt.htmlText = s;
//		}
		
//		private function iOError(event:IOErrorEvent):void {
//			trace(event);
//		}
		
//		public function onLoad(event:Event):void {
//			trace(LoaderInfo(event.target).url);
//            ii++;
//            trace("loaded: ", ii);
//        	createBitmap(Bitmap(LoaderInfo(event.target).content));
//        }
        
//		private function getHandler(i:uint):Function {
//			trace("getHandler ", i);
//		    return function(event:Event):void {
//				trace("fun ", LoaderInfo(event.target).url, " ", i);
//	            ii++;
//	            trace("loaded: ", ii);
//	        	createBitmap(Bitmap(LoaderInfo(event.target).content));
//		    }
//		}
        
        
        private var xx:int = 0;
        private var yy:int = 0;
        
        private function createBitmap(bmp:Bitmap):void {
        	trace("cb");
            var rect:Rectangle = new Rectangle(0, 0, 150, 150);
            var pt:Point;
//        	for each (var bmp:Bitmap in v) {
        		if (xx == 10) {
        			xx = 0;
        			yy++;
        		}
	            pt = new Point(xx * 150, yy * 150);
	            _bitmap.copyPixels(bmp.bitmapData, rect, pt);
	            xx++;
//        	}
        }
		
		public function f(i:uint):void {
//			trace("f: ", i);
			view(_items[i]);
		}
		
		private function view(...targets:Array):void {
			for each (var sel:Object in land.selection) {
				if (sel is Hilight) {
					Hilight(sel).currentState = null;	
				}
			}
			land.selection = targets;
			for each (sel in land.selection) {
				if (sel is Hilight) {
					Hilight(sel).currentState = "selected";	
				}
			}
		} 
		
	}
	
}